package com.aopcinemas.cinema.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import com.aopcinemas.cinema.dao.HallDao;
import com.aopcinemas.cinema.dao.MovieDao;
import com.aopcinemas.cinema.dao.ScheduleDao;
import com.aopcinemas.cinema.event.ScheduleListListener;
import com.aopcinemas.cinema.model.Hall;
import com.aopcinemas.cinema.model.Movie;
import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.common.ui.UIUtils;
import com.toedter.calendar.JDateChooser;

public class ScheduleCreationDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private ArrayList<ScheduleListListener> listeners = new ArrayList<ScheduleListListener>();

    private JList movieList;
    private JList hallList;
    private JDateChooser date;
    private JFormattedTextField time = new JFormattedTextField(new SimpleDateFormat("HH:mm"));
    private JSpinner priceSpinner = new JSpinner(new SpinnerNumberModel(0.0, 0.0, Double.MAX_VALUE, 0.5));

    public ScheduleCreationDialog(JFrame owner, final ScheduleDao scheduleDao, MovieDao movieDao, HallDao hallDao) {
        super(owner, true);

        List<Movie> movies = movieDao.getMovies();
        List<Hall> halls = hallDao.getHalls();

        movieList = new JList(movies.toArray());
        hallList = new JList(halls.toArray());
        date = new JDateChooser();
        time.setColumns(10);
        priceSpinner.setPreferredSize(new Dimension(100, 20));

        JButton addSchedule = new JButton("Add schedule");
        addSchedule.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Schedule schedule = new Schedule();
                schedule.setMovie((Movie) movieList.getSelectedValue());
                schedule.setHall((Hall) hallList.getSelectedValue());
                schedule.setBasePrice((Double) priceSpinner.getValue());
                schedule.setDate(UIUtils.mergeDateAndTime(date.getDate(), (Date) time.getValue()));

                scheduleDao.addSchedule(schedule);
                notifyListeners();
            }
        });

        JPanel panel = new JPanel();

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(3, 3, 3, 3);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        panel.setLayout(layout);

        // Movie list
        JLabel label = new JLabel("Movie:");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHEAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        JScrollPane scroll = new JScrollPane(movieList);
        scroll.setPreferredSize(new Dimension(250, 100));
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(scroll, constraints);
        panel.add(scroll);

        // Hall list
        label = new JLabel("Hall:");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHEAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        scroll = new JScrollPane(hallList);
        scroll.setPreferredSize(new Dimension(250, 100));
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(scroll, constraints);
        panel.add(scroll);

        // price
        label = new JLabel("price:");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(priceSpinner, constraints);
        panel.add(priceSpinner);

        label = new JLabel("Time:");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        // Time
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(time, constraints);
        time.setText("12:00");
        panel.add(time);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(date, constraints);
        date.setDate(new Date());
        panel.add(date);

        constraints.anchor = GridBagConstraints.CENTER;
        layout.setConstraints(addSchedule, constraints);
        panel.add(addSchedule);

        getContentPane().add(panel);

        setSize(new Dimension(400, 400));
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    public void addScheduleListListener(ScheduleListListener listener) {
        listeners.add(listener);
    }

    public void removeScheduleListListener(ScheduleListListener listener) {
        listeners.remove(listener);
    }

    private void notifyListeners() {
        for (ScheduleListListener listener : listeners) {
            listener.listUpdated();
        }
    }
}
